/* 
This file is part of Ingo Karstein's Excel Export project

**Do not remove this comment**

Please see the project homepage at CodePlex:
  http://spexcelexport.codeplex.com/

Please see my blog:
  http://ikarstein.wordpress.com

Licence: GPLv2
    Ingo Karstein's SharePoint 2010 Custom Ribbon Demo
    Copyright (C) 2011  Ingo Karstein, ikarstein@hotmail.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using System.Reflection;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;

namespace ik.SharePoint2010.ExcelExport.Features
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("a03fb9b1-b82c-4777-8827-d522ac130316")]
    public class ExcelExportEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            //Debugger.Break();

            SPWeb web = properties.Feature.Parent as SPWeb;

            if( web != null )
            {
                SPList list = web.GetList(web.ServerRelativeUrl + "/Lists/ExcelExportTemplates");
                if( list != null )
                {
                    list.BreakRoleInheritance(false);
                    list.Update();

                    Assembly r = Assembly.GetExecutingAssembly();
                    Stream inp = r.GetManifestResourceStream("ik.SharePoint2010.ExcelExport.Resources.ExcelExportTemplate.xlsx");

                    /*                    
                        byte[] data = new byte[0];

                        int readBytes = 0;
                        byte[] buf = new byte[1000000];
                        do
                        {
                            readBytes = inp.Read(buf, 0, buf.Length);
                            if( readBytes > 0 )
                            {
                                byte[] newData = new byte[data.Length + readBytes];
                                data.CopyTo(newData, 0);
                                buf.CopyTo(newData, data.Length);
                                data = newData;
                            }
                        } while( readBytes > 0 );
                        inp.Close();
                     */

                    try
                    {
                        SPFile template = list.RootFolder.Files.Add("template.xlsx", inp);
                        SPListItem i = template.Item;
                        i["Title"] = "Template";
                        i.Update();
                        list.Update();
                    }
                    finally
                    {
                        inp.Close();
                    }
                }
            }
        }


        // Uncomment the method below to handle the event raised before a feature is deactivated.

        //public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
